﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AspNetCs.Common;
using AspNetCs.Model;
using AspNetCs.Bll;
using AspNetCs.Enumerations;
using AspNetCs.Enumerations.User_Enum;
using AspNetCs.Enumerations.memorial_enum;
using System.Reflection;

public partial class Users_EditNetMu : BasePage
{
    protected string DefaultValue = "";
    protected string t = "1";

    protected int id = 0;
    protected NetMuBLL bll = new NetMuBLL();
    protected NetMu_Model model = new NetMu_Model();
    protected memorial_model MemorialModel = new memorial_model();
    protected Memorial MemorialBll = new Memorial();
    protected Memorial_Type_Model MemorialTypeModel = new Memorial_Type_Model();

    //protected int CurrTypeInt = 1;
    //protected int EnumTypeInt = (int)Person_TypeInt_Enum.both;
    //protected int IsPwdUI = 0;
    //protected string PwdShow = "";

    protected string city;
    protected string province;

    private int userid = 0;

    protected void Page_Load(object sender, EventArgs e)
    {
        
        CheckUserExit("Login.Aspx", "请您登录", true);
        userid = Base_GetUserInfo.UserId;
        if (Request["id"] != null)
        {
            id = StringHelper.isNumber_int(Request["id"].ToString());
        }
        if (id <= 0)
        {
            ShowMsgBack("参数传递错误！");
            Response.End();
        }

        Base_CurrPath = Base_CurrPath + " >> 会员管理中心 >> 公墓管理 >> 修改信息";
        CheckUserExit("Login.Aspx?ReUrl=" + configInfo.InstallDir + "Users/EditNetMu_" + id + ".Aspx", "", true);

        model=bll.getNetMuDetailInfo(id);
        if (Base_GetLoginInfo.GetUserID != model.CreateId)
        {
            ShowMsgBack("当前公墓不属于您创建,你暂时不能对她进行管理");
            Response.End();
        }
        if (!Page.IsPostBack)
        {
            BindKindsOfDataControl();
            //EditBind();
            initPage();
            changeMuType();
        }
    }

    private void BindKindsOfDataControl()
    {
        EnumDescription[] Sex_Ed = EnumDescription.GetFieldTexts(typeof(Sex_Enum), EnumDescription.SortType.Rank);
        EnumDescription[] Ed = EnumDescription.GetFieldTexts(typeof(TimeType_enum), EnumDescription.SortType.Rank);

        Dictionary<string, string> dicNetMuTypes = BindEnumList<NetMuTypeEnum>.BindEnum();

        ddlMuType.DataSource = dicNetMuTypes;
        ddlMuType.DataTextField = "value";
        ddlMuType.DataValueField = "key";
        ddlMuType.DataBind();

        TwoPanel.Visible = false;

        this.Radio_Sex.DataTextField = "EnumDisplayText";
        this.Radio_Sex.DataValueField = "EnumDisplayText";
        this.Radio_Sex.DataSource = Sex_Ed;
        this.Radio_Sex.DataBind();
        this.Radio_Sex.SelectedIndex = 0;
        this.Radio_Sex.RepeatColumns = 5;

        this.ddlSexTwo.DataTextField = "EnumDisplayText";
        this.ddlSexTwo.DataValueField = "EnumDisplayText";
        this.ddlSexTwo.DataSource = Sex_Ed;
        this.ddlSexTwo.DataBind();
        this.ddlSexTwo.SelectedIndex = 0;
        this.ddlSexTwo.RepeatColumns = 5;





        this.DD_TimeType.DataTextField = "EnumDisplayText";
        this.DD_TimeType.DataValueField = "EnumDisplayText";
        this.DD_TimeType.DataSource = Ed;
        this.DD_TimeType.DataBind();
        this.DD_TimeType.SelectedIndex = 0;

        this.DD_DeathType.DataTextField = "EnumDisplayText";
        this.DD_DeathType.DataValueField = "EnumDisplayText";
        this.DD_DeathType.DataSource = Ed;
        this.DD_DeathType.DataBind();
        this.DD_DeathType.SelectedIndex = 0;




        this.Txt_birChangeTwo.DataTextField = "EnumDisplayText";
        this.Txt_birChangeTwo.DataValueField = "EnumDisplayText";
        this.Txt_birChangeTwo.DataSource = Ed;
        this.Txt_birChangeTwo.DataBind();
        this.Txt_birChangeTwo.SelectedIndex = 0;

        this.DD_DeathTimeTwo.DataTextField = "EnumDisplayText";
        this.DD_DeathTimeTwo.DataValueField = "EnumDisplayText";
        this.DD_DeathTimeTwo.DataSource = Ed;
        this.DD_DeathTimeTwo.DataBind();
        this.DD_DeathTimeTwo.SelectedIndex = 0;



    }

    protected void initPage()
    {
        NetMu_Model netMuModel = new NetMu_Model();
        NetMuBLL netMuBll = new NetMuBLL();
        netMuModel = netMuBll.getNetMuDetailInfo(id);

        txtNetMuId.Text = netMuModel.MuId;
        txtNetMuName.Text = netMuModel.MuName;
        Txt_UserNa.Text = netMuModel.Susername;
        Txt_UserName.Text = netMuModel.Username;
        Txt_otherName.Text = netMuModel.OtherName;
        Txt_UpImg.Text = netMuModel.Picture;
        Radio_Sex.SelectedValue = netMuModel.Sex;
        Txt_Race.Text = netMuModel.Nation;
        Txt_place.Text = netMuModel.Place;
        Txt_Occu.Text = netMuModel.Occupation;
        Txt_Belief.Text = netMuModel.Belief;
        Txt_birthyear.Text = netMuModel.Birthday;
        Txt_birthyear.Text=netMuModel.LunarCarlendar_birth;
        DD_TimeType.SelectedValue = netMuModel.BirthType;
        DD_DeathType.SelectedValue = netMuModel.DepartType;
        
        if (netMuModel.BirthType == TimeType_enum.pub.ToString())
        {
            Txt_birthyear.Text =netMuModel.Birthday.ToString().Trim();
            if (StringHelper.isDatetime_Bool(netMuModel.Birthday))
            {
                model.LunarCarlendar_birth = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(netMuModel.Birthday));
            }
        }
        else
        {
            Txt_birthyear.Text = netMuModel.LunarCarlendar_birth.ToString().Trim();
        }

        if (netMuModel.DepartType == TimeType_enum.pub.ToString())
        {
            Txt_DeathTime.Text = netMuModel.DepartTime.ToString().Trim();

            if (StringHelper.isDatetime_Bool(netMuModel.DepartTime))
            {
                netMuModel.LunarCalendar_depart = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(netMuModel.DepartTime));
            }
        }
        else
        {
            Txt_DeathTime.Text = netMuModel.LunarCalendar_depart.ToString().Trim();
        }


        Txt_SnameTwo.Text = netMuModel.Surname_Two;
        Txt_unameTwo.Text = netMuModel.Username_Two;
        Txt_OtherNameTwo.Text = netMuModel.OtherName_Two;
        Txt_UppicTwo.Text = netMuModel.Picture_Two;
        ddlSexTwo.SelectedValue = netMuModel.Sex_two;
        Txt_RaceTwo.Text = netMuModel.Nation_two;
        Txt_placeTwo.Text = netMuModel.Place_Two;
        Txt_OccuTwo.Text = netMuModel.Occupation_Two;
        Txt_BeliefTwo.Text = netMuModel.Belief_Two;
        Txt_birthTwo.Text = netMuModel.Birthday_Two;
        Txt_birthTwo.Text = netMuModel.LunarCalendar_birth_Two;
        Txt_DeathTimeTwo.Text = netMuModel.DepartTime_Two;
        Txt_DeathTimeTwo.Text = netMuModel.LunarCalendar_depart_Two;


        Txt_birChangeTwo.SelectedValue = netMuModel.BirthType_Two;
        if (netMuModel.BirthType_Two == TimeType_enum.pub.ToString())
        {
            Txt_birthTwo.Text = netMuModel.Birthday_Two;

            if (StringHelper.isDatetime_Bool(netMuModel.Birthday_Two))
            {
                netMuModel.Birthday_Two = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(netMuModel.LunarCalendar_birth_Two));
            }
        }
        else
        {
            Txt_birthTwo.Text = netMuModel.LunarCalendar_birth_Two;
        }

        DD_DeathTimeTwo.SelectedValue = netMuModel.DepartType_Two;

        if (netMuModel.DepartType_Two == TimeType_enum.pub.ToString())
        {
            Txt_DeathTime.Text = netMuModel.DepartTime_Two;

            if (StringHelper.isDatetime_Bool(netMuModel.DepartTime_Two))
            {
                netMuModel.LunarCalendar_depart_Two = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(netMuModel.DepartTime_Two));
            }

        }
        else
        {
            Txt_DeathTime.Text = netMuModel.LunarCalendar_depart_Two;
        }

        WebEditor1.Text = netMuModel.Readme;
        ddlMuType.SelectedValue = netMuModel.MuType.ToString();
        ddlMusic.SelectedValue = netMuModel.MusicId.ToString();
        province = model.Province.ToString();
        city = model.City.ToString();

        //if (Request["selProvice"] != null)
        //{
        //    model.Province = StringHelper.isNumber_int(Request["selProvice"].ToString());

        //}
        //if (model.Province > 0)
        //{
        //    if (Request["selCity"] != null)
        //    {
        //        model.City = StringHelper.isNumber_int(Request["selCity"].ToString());
        //    }
        //}
    }

    public void update()
    {
        Users_Model userModel = Base_GetUserInfo;
        NetMu_Model model = new NetMu_Model();
        model.Id = id;
        model.MuId = txtNetMuId.Text;
        model.MuName = txtNetMuName.Text;
        model.CreateId = userModel.UserId;
        model.Susername = Txt_UserNa.Text;
        model.Username = Txt_UserName.Text;
        model.OtherName = Txt_otherName.Text;
        model.Picture = Txt_UpImg.Text;
        model.Sex = Radio_Sex.SelectedValue;
        model.Nation = Txt_Race.Text;
        model.Place = Txt_place.Text;
        model.Occupation = Txt_Occu.Text;
        model.Belief = Txt_Belief.Text;
        model.Birthday = Txt_birthyear.Text;
        model.LunarCarlendar_birth = Txt_birthyear.Text;
        model.DepartTime = Txt_DeathTime.Text;
        model.LunarCalendar_depart = Txt_DeathTime.Text;
        model.BirthType = DD_TimeType.SelectedValue;
        if (model.BirthType == TimeType_enum.pub.ToString())
        {
            model.Birthday = StringHelper.HtmlEncrypt(Txt_birthyear.Text.ToString().Trim());
            if (StringHelper.isDatetime_Bool(model.Birthday))
            {
                model.LunarCarlendar_birth = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(model.Birthday));
            }

        }
        else
        {
            model.LunarCarlendar_birth = StringHelper.HtmlEncrypt(Txt_birthyear.Text.ToString().Trim());
        }



        model.DepartType = DD_DeathType.SelectedValue;

        if (model.DepartType == TimeType_enum.pub.ToString())
        {
            model.DepartTime = StringHelper.HtmlEncrypt(Txt_DeathTime.Text.ToString().Trim());

            if (StringHelper.isDatetime_Bool(model.DepartTime))
            {
                model.LunarCalendar_depart = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(model.DepartTime));
            }
        }
        else
        {
            model.LunarCalendar_depart = StringHelper.HtmlEncrypt(Txt_DeathTime.Text.ToString().Trim());
        }


        model.Surname_Two = Txt_SnameTwo.Text;
        model.Username_Two = Txt_unameTwo.Text;
        model.OtherName_Two = Txt_OtherNameTwo.Text;
        model.Picture_Two = Txt_UppicTwo.Text;
        model.Sex_two = ddlSexTwo.SelectedValue;
        model.Nation_two = Txt_RaceTwo.Text;
        model.Place_Two = Txt_placeTwo.Text;
        model.Occupation_Two = Txt_OccuTwo.Text;
        model.Belief_Two = Txt_BeliefTwo.Text;
        model.Birthday_Two = Txt_birthTwo.Text;
        model.LunarCalendar_birth_Two = Txt_birthTwo.Text;
        model.DepartTime_Two = Txt_DeathTimeTwo.Text;
        model.LunarCalendar_depart_Two = Txt_DeathTimeTwo.Text;


        model.BirthType_Two = Txt_birChangeTwo.SelectedValue;
        if (model.BirthType_Two == TimeType_enum.pub.ToString())
        {
            model.Birthday_Two = StringHelper.HtmlEncrypt(Txt_birthTwo.Text.ToString().Trim());

            if (StringHelper.isDatetime_Bool(model.Birthday_Two))
            {
                model.LunarCalendar_birth_Two = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(model.Birthday_Two));
            }
        }
        else
        {
            model.LunarCalendar_birth_Two = StringHelper.HtmlEncrypt(Txt_birthTwo.Text.ToString().Trim());
        }

        model.DepartType_Two = DD_DeathTimeTwo.SelectedValue;

        if (model.DepartType_Two == TimeType_enum.pub.ToString())
        {
            model.DepartTime_Two = StringHelper.HtmlEncrypt(Txt_DeathTime.Text.ToString().Trim());

            if (StringHelper.isDatetime_Bool(model.DepartTime_Two))
            {
                model.LunarCalendar_depart_Two = StringHelper.ChineseLunisolarDate(StringHelper.isDatetime(model.DepartTime_Two));
            }

        }
        else
        {
            model.LunarCalendar_depart_Two = StringHelper.HtmlEncrypt(Txt_DeathTime.Text.ToString().Trim());
        }

        model.Readme = WebEditor1.Text;
        model.MuType = StringHelper.isNumber_int(ddlMuType.SelectedValue);
        model.MusicId = StringHelper.isNumber_int(ddlMusic.SelectedValue);
        model.CreateDate = DateTime.Now;
        if (Request["selProvice"] != null)
        {
            model.Province = StringHelper.isNumber_int(Request["selProvice"].ToString());

        }
        if (model.Province > 0)
        {
            if (Request["selCity"] != null)
            {
                model.City = StringHelper.isNumber_int(Request["selCity"].ToString());
            }
        }
        model.Hot = 0;

        model.NetIp = StringHelper.Ip();
        model.IsCheck = 0;
        model.IsDel = 0;
        model.TemplateId = StringHelper.isNumber_int(Request["templateId"]);
        decimal TemplatePoint = StringHelper.isNumber_decimal(Request["hldTemplePoint"]);

        bll.updateNetMu(model);

        if (this.model.TemplateId != model.TemplateId && model.TemplateId!=0)
        {
            Temple_Model TmpMd = TempleBll.CreateNew.GetModel( model.TemplateId);
            if (TemplatePoint!=0)
            {

                if (TemplatePoint <= Base_GetUserInfo.Point)
                {
                    Point_Log_Model pointLog = new Point_Log_Model();
                    pointLog.Addtime = DateTime.Now;
                    pointLog.NetIp = StringHelper.Ip();
                    pointLog.Point = TemplatePoint;
                    pointLog.Readme = "用户修改公墓模板为" + TmpMd.TempleName+ ",扣除" + TemplatePoint + "点";
                    pointLog.UserId = this.userid;
                    pointLog.UserType =(int) UserTypeEnum.User;
                    Point_Log_Bll.CreateNew.Add(pointLog);
                }
                else
                {
                    Response.Write("<script>alert('保存失败,用户点数不足!');location.href='EditNetMu.aspx';</script>");
                    return;
                }
                
            }
        }

        

    }

    protected void ddlMuType_SelectedIndexChanged(object sender, EventArgs e)
    {
        changeMuType();
    }

    protected void changeMuType()
    { 
        int sel = Convert.ToInt32(ddlMuType.SelectedValue.ToString());
        if (sel == (int)NetMuTypeEnum.单人墓)
        {
            TwoPanel.Visible = false;
        }
        else if (sel == (int)NetMuTypeEnum.双人墓)
        {
            TwoPanel.Visible = true;
        }
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        update();
        Response.Redirect("NetMuManage.aspx?id="+id);
    }
}
